package com.wangsd.web.sys.service;

import com.gitee.fastmybatis.core.query.Query;
import com.gitee.fastmybatis.core.query.Sort;
import com.wangsd.common.utils.OssFileUtils;
import com.wangsd.web.sys.entity.SysButton;
import com.wangsd.web.sys.entity.UploadFile;
import com.wangsd.web.sys.mapper.SysButtonMapper;
import com.wangsd.web.sys.mapper.UploadFileMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UploadFileService {
    @Resource
    UploadFileMapper mapper;

    public int deleteByFileName(String fileName) {
        return mapper.deleteByQuery(new Query().eq("file_name", fileName));
    }

    public void deleteByMasterIdAndType(Long masterId, Integer masterType) {
        List<UploadFile> list = selectByMaster(masterId, masterType);
        for (UploadFile file : list) {
            OssFileUtils.deleteFile(file.getFilename());
            mapper.deleteById(file.getId());
        }
    }
    public UploadFile selectByFileName(String fileName) {
        return mapper.getByColumn("file_name", fileName);
    }

    public List<UploadFile> selectByMaster(Long masterId, Integer masterType) {
        Query query = new Query();
        query.eq("master_id", masterId);
        query.eq("master_type", masterType);
        return mapper.list(query);
    }

    public List<UploadFile> selectSwiper(Long masterId, Integer masterType) {
        Query query = new Query();
        query.eq("master_id", masterId);
        query.eq("master_type", masterType);
        query.eq("status", 2);
        query.addSort("sort_no", Sort.DESC);
        return mapper.list(query);
    }
    public List<UploadFile> selectPage(UploadFile uploadFile, String sortField, String sortOrder) {
        Query query = new Query();
        if (uploadFile.getMasterId() != null) {
            query.eq("master_id", uploadFile.getMasterId());
        }
        if (StringUtils.isNotBlank(uploadFile.getName())) {
            query.like("name", uploadFile.getName());
        }
        query.eq("master_type", uploadFile.getMasterType());
        if (StringUtils.isNotBlank(sortField) && StringUtils.isNotBlank(sortOrder)) {
            query.orderby(sortField, "ascend".equals(sortOrder) ? Sort.ASC : Sort.DESC);
        }
        query.orderby("sort_no", Sort.DESC);
        return mapper.list(query);
    }

    public List<UploadFile> selectByDelete() {
        return mapper.listByColumn("is_deleted", true);
    }


    public UploadFile saveUploadFile(String fileName, String fileUrl) {
        UploadFile uploadFile = new UploadFile();
        uploadFile.setFilename(fileName);
        uploadFile.setFileUrl(fileUrl);
        uploadFile.setStatus(1);
        uploadFile.setIsDeleted(true);
        mapper.saveIgnoreNull(uploadFile);
        return uploadFile;
    }

    /**
     * 单独上传图片的方法
     *
     * @param file
     * @return
     */
    @Transactional
    public int updateUploadFile(UploadFile file) {
        //id不等于null,就是编辑
        if (file.getId() != null) {
            //不等于null,就是修改了图片,修改了图片需要把老图片删掉
            if (file.getFilename() != null) {
                //file.getFilename()有值,就是刚替换了图片 查询出刚新增的图片
                UploadFile newObj = selectByFileName(file.getFilename());
                //设置新图片的地址
                file.setFilename(newObj.getFilename());
                file.setFileUrl(newObj.getFileUrl());
                //根据id查询出原来的图片
                UploadFile old = mapper.getById(file.getId());
                //根据原来图片的filename删除阿里云里面的图片
                OssFileUtils.deleteFile(old.getFilename());
                //删掉刚上传的图片记录
                mapper.deleteById(newObj.getId());
            } else {
                //相等,就是没有修改图片
            }
        } else {
            //新增
            UploadFile newObj = selectByFileName(file.getFilename());
            file.setId(newObj.getId());
        }
        file.setIsDeleted(false);
        return mapper.updateIgnoreNull(file);
    }

    /**
     * 跟主体同时上传图片的方法
     *
     * @param file
     * @return
     */
    @Transactional
    public String updateUploadFile2(UploadFile file) {
        UploadFile oldObj = null;
        List<UploadFile> oldList = this.selectByMaster(file.getMasterId(), file.getMasterType());
        if (oldList != null && oldList.size() > 0) {
            //oldList有值,说明是修改图片
            oldObj = oldList.get(0);
            //根据原来图片的filename删除阿里云里面的图片
            OssFileUtils.deleteFile(oldObj.getFilename());
            //查询出刚新增的图片
            UploadFile newObj = selectByFileName(file.getFilename());
            //设置新图片的地址
            oldObj.setFilename(newObj.getFilename());
            oldObj.setFileUrl(newObj.getFileUrl());
            //删掉刚上传的图片记录
            mapper.deleteById(newObj.getId());
        }else {
            oldObj = this.selectByFileName(file.getFilename());
            oldObj.setMasterType(file.getMasterType());
            oldObj.setMasterId(file.getMasterId());
        }
        oldObj.setIsDeleted(false);
        mapper.updateIgnoreNull(oldObj);
        return oldObj.getFileUrl();
    }

    public void updateStatus(Long id) {
        UploadFile uploadFile = mapper.getById(id);
        uploadFile.setStatus(uploadFile.getStatus() == 1 ? 2 : 1);
        mapper.update(uploadFile);
    }

    public List<UploadFile> selectGrids(List<Long> grids) {
        Query query = new Query();
        query.in("id", grids);
        return mapper.list(query);
    }

}
